from django.core.mail import send_mail

from accounts.models import Department_Mode
from django.conf import settings


def fault_send_mail(
        incident_user=None, mailcomment=None, get_status_display=None,
        starttime=None, scantime=None,
        stoptime=None, content=None,
        project_principal=None, mail_title=None, sender_form=None):
    """
    :param incident_user:           故障创建人
    :param mailcomment:             报警内容(主题)
    :param get_status_display:      故障处理状态 (处理中,未处理, 关闭)
    :param starttime:               发生时间
    :param scantime:                发现时间
    :param stoptime:                结束时间
    :param content:                 内容
    :param project_principal:       项目负责人
    :param mail_title:
    :param sender_form:             发件人 (settings中获取)
    :return:
    """

    mail_msg = '''<!DOCTYPE html>
<html>
    <head>
        <head data-suburl="">
            <meta http-equiv="Content-Type" content="text/html; charset=UTF-8"/>
            <meta http-equiv="X-UA-Compatible" content="IE=edge"/>
            <meta name="author" content="运维管理系统"/>
            <meta name="viewport" content="width=device-width, initial-scale=1.0">
            <title>运维自动化平台</title>
            <!-- 新 Bootstrap 核心 CSS 文件 -->
            <link rel="stylesheet" href="http://cdn.bootcss.com/bootstrap/3.3.5/css/bootstrap.min.css">
            <!-- 可选的Bootstrap主题文件（一般不用引入） -->
            <link rel="stylesheet" href="http://cdn.bootcss.com/bootstrap/3.3.5/css/bootstrap-theme.min.css">
            <!-- jQuery文件。务必在bootstrap.min.js 之前引入 -->
            <script src="http://cdn.bootcss.com/jquery/1.11.3/jquery.min.js"></script>
            <!-- 最新的 Bootstrap 核心 JavaScript 文件 -->
            <script src="http://cdn.bootcss.com/bootstrap/3.3.5/js/bootstrap.min.js"></script>
        </head>
    <body>
        <div class="container-fluid">
            <div class="row">
                <div class="col-md-12">
                    <div>
                        <h2>故障详情如下:</h2>
                        <p>报障人员: %s</p>
                        <p>报警内容: %s</p>
                        <p>处理状态: %s</p>
                        <p>发生时间: %s</p>
                        <p>发现时间: %s</p>
                        <p>解决时间: %s</p>
                        <p>解决过程: %s</p>
                    </div>
                </div>
                <div class="col-md-12">
                    <div class="alert alert-success alert-dismissable">
                        <h4>提醒!</h4>
                        此邮件为系统自动发送,请勿回复
                          
                    </div>
                    <p>关注运维团队微信,可接收微信报警通知</p>
                </div>
            </div>
        </div>
    </body>
</html>
''' % (
        incident_user, mailcomment, get_status_display, starttime, scantime,
        stoptime, content)

    mail_list = []
    ops_mail = []
    ops_mail_list = []

    # 获取运维组所有成员
    dep_obj = Department_Mode.objects.filter(desc_gid=1001)  # todo 硬编码
    if dep_obj.exists():
        dep_obj = dep_obj.first()
        ops_mail = dep_obj.users.values_list('email', flat=True)
        ops_mail = list(ops_mail) if ops_mail else []
    if ops_mail:
        ops_mail_list.extend(ops_mail)
        mail_list.extend(ops_mail_list)

    if project_principal:
        # 是否通知项目负责人
        mail_list.append(project_principal)

    if not mail_list:
        # 如果都没有,则会使用默认的邮件
        mail_list.append(settings.EMAIL_HOST_USER)
    mail_list = set(mail_list)  # 去重
    sender_form = settings.EMAIL_HOST_USER
    send_mail(mail_title, "故障报警", sender_form, mail_list, fail_silently=False, html_message=mail_msg)
